package graph_interface;

import java.io.*;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import java.sql.*;

public class Registered extends Application{
	
	
	public static void main(String[] args) {
		Application.launch(args);
	}
	private Label[] lab=new Label[10];
	private final TextField name=new TextField();
	private final PasswordField pw=new PasswordField();
	RadioButton rb1=new RadioButton("男");
	RadioButton rb2=new RadioButton("女");
	TextField age=new TextField();
	Button bt1=new Button("注册");
	
	@Override
	public void start(Stage stage) throws Exception {
		lab[0]=new Label("姓名");
		lab[1]=new Label("密码");
		lab[2]=new Label("性别");
		lab[3]=new Label("年龄");
		lab[4]=new Label("身高");
		lab[5]=new Label("体重");
		lab[6]=new Label("代表队");
		lab[7]=new Label("id");
		TextField height=new TextField();
		TextField weight=new TextField();
		TextField team=new TextField();
		TextField id=new TextField();
		ToggleGroup tg=new ToggleGroup();
		rb1.setToggleGroup(tg);
		rb2.setToggleGroup(tg);
		HBox hb=new HBox(rb1,rb2);
		hb.setSpacing(30);
		GridPane root=new GridPane();
		root.setPadding(new Insets(10));
		root.setAlignment(Pos.TOP_CENTER);
		root.setHgap(10);
		root.setVgap(10);
		root.add(lab[0], 0, 0);
		root.add(name, 1, 0);
		root.add(lab[1], 0, 1);
		root.add(pw,1,1);
		root.add(lab[2],0,2);
		root.add(hb,1,2);
		root.add(lab[3],0,3);
		root.add(age,1,3);
		root.add(lab[4],0,4);
		root.add(height,1,4);
		root.add(lab[5],0,5);
		root.add(weight,1,5);
		root.add(lab[6],0,6);
		root.add(team,1,6);
		
		root.add(lab[7],0,7);
		root.add(id,1,7);
		
		root.add(bt1,1,8);
		bt1.setOnAction(event->
		{
			try(FileWriter fr=new FileWriter("database/user.txt",true);)
			{
				//写入文件
				String str=name.getText();
				String p=pw.getText();
				String sex="女",Tname="";
				int a=Integer.parseInt(id.getText());
				int b=Integer.parseInt(age.getText());
				double h=Double.parseDouble(height.getText());
				double w=Double.parseDouble(weight.getText());
				Tname=team.getText();
				if(rb1.isSelected())
				{
					sex="男";
				}
				if(str.length()==0||p.length()==0)
				{
					System.out.println("用户名或密码不能为空");
					return;
				}
				str=str+" "+p+"\n";
				fr.write(str);
				bt1.setText("注册成功");
				bt1.setTextFill(Color.GREEN);
				
				//写入数据库
				Connection conn=DBConnection.getConnection();
				PreparedStatement ps=conn.prepareStatement("insert into athletes values(?,?,?,?,?,?,?);");
				ps.setInt(1,a);
				ps.setString(2,name.getText());
				ps.setInt(3,b);
				ps.setString(4,sex);
				ps.setDouble(5,h);
				ps.setDouble(6,w);
				ps.setString(7,Tname);
				ps.executeUpdate();
			}
			catch(FileNotFoundException e) {
				System.out.println("文件未找到");
			}
			catch(SQLException e) {
				
			}
			catch(IOException e) {
				System.out.println("错误");
			}
		});
		Scene scene=new Scene(root,300,500);
		stage.setTitle("注册表单");
		stage.setScene(scene);
		stage.show();
	}
	
}
